package com.deke.course.service.Impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.deke.course.vo.MyListPage;
import com.deke.course.vo.Page;
import com.deke.entity.EduSubject;



import com.deke.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.deke.course.dao.EduSubjectDao;
import com.deke.course.service.EduSubjectService;
import com.deke.course.vo.SubjectVo;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class EduSubjectServiceImpl implements EduSubjectService {
    @Autowired
    private EduSubjectDao subjectDao;


    /**
     * 分页查询+模糊查询
     * @Author Lim
     * @Date 2022/12/9 14:24
     */

    @Override
    public Result findByConditionPage(SubjectVo subjectVo, Integer Current, Integer pageSize) {
        List<EduSubject> eduSubjects =null;
        QueryWrapper<EduSubject> wrapper=new QueryWrapper<>();
        if (StringUtils.hasText(subjectVo.getTitle())){
            wrapper.like("title",subjectVo.getTitle());
            eduSubjects=subjectDao.selectList(wrapper);
        }else{
            eduSubjects=subjectDao.selectList(null);
        }
        //1. 查询一级菜单
        List<EduSubject> first=new ArrayList<>();
        for(EduSubject eduSubject:eduSubjects){
            if(eduSubject.getParentId().equals("0")){
                first.add(eduSubject);
            }
        }
        //2. 查询一级菜单下的子菜单
        for(EduSubject f:first){
            f.setChildren(findChildren(eduSubjects,f.getId()));
        }
        Page page = MyListPage.startPage(first, Current, pageSize);

        return new Result(200,"分页查询课程分类成功",page);

    }
    /**
     * @param id
     * 删除操作
     * @Author Lim
     * @Date 2022/12/9 22:19
     */
    @Override
    public Result deleteSubject(String id) {
        int i = subjectDao.deleteById(id);
        if( i>0) {
            return new Result(200, "删除成功");
        }
        return new Result(000, "删除失败");
    }



    private List<EduSubject> findChildren(List<EduSubject> eduSubjects, String id){
        List<EduSubject> senconds=new ArrayList<>();
        for (EduSubject p:eduSubjects){
            if(p.getParentId().equals(id)){
                senconds.add(p);
            }
        }
        for (EduSubject p:senconds){
            p.setChildren(findChildren(eduSubjects,p.getId()));
        }
        return senconds;
    }
    /**
      * @param eduSubject
     * 添加操作
     * @Author Lim
     * @Date 2022/12/9 22:20
     */
    @Override
    public Result insertSubject(EduSubject eduSubject) {
        if(eduSubject.getId().equals("0")&&eduSubject.getParentId().equals("0")){
            eduSubject.setId(null);
            eduSubject.setGmtCreate(LocalDateTime.now());
            eduSubject.setGmtModified(LocalDateTime.now());
            int i = subjectDao.insert(eduSubject);
            if(i>0){
                return new Result(200,"新增成功");
            }
            return new Result(000,"新增失败");
        }
        else{
            String prentId = eduSubject.getId();
            eduSubject.setParentId(prentId);
            eduSubject.setId(null);
            eduSubject.setGmtCreate(LocalDateTime.now());
            eduSubject.setGmtModified(LocalDateTime.now());
            int i = subjectDao.insert(eduSubject);
            if(i>0){
                return new Result(200,"新增成功");
            }
            return new Result(000,"新增失败");
        }

    }

    @Override
    public Result updatesubject(EduSubject eduSubject) {
        int i = subjectDao.updateById(eduSubject);

        if(i>0){
            return new Result(200,"修改成功");
        }
        return new Result(000,"修改失败");
    }

    @Override
    public Result changeStatusId(Map map) {
       EduSubject eduSubject1=new EduSubject();
       eduSubject1.setId(map.get("id").toString());
       eduSubject1.setParentId(map.get("parentId").toString());
       eduSubject1.setStatusId(map.get("statusId").toString());
        int update = subjectDao.updateById(eduSubject1);
        if(update>0){
            return new Result(200,"修改状态成功");
        }
        return new Result(000,"修改状态成功失败");
    }


    /**
     * @create by: Username 刘亦辰 
     * @description: 前台  获取所有菜单 
     * @create time: 2022/12/21 22:39
     * @param null
     * @return :  
     **/
    @Override
    public Result getAllMenu() {
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("status_id",1);
        List<EduSubject> subjectList = subjectDao.selectList(wrapper);
        List<EduSubject> first=new ArrayList<>();
        for (EduSubject subject : subjectList) {
            if (subject.getParentId().equals("0")){
                first.add(subject);
            }
        }
        for (EduSubject f : first) {
            f.setChildren(findChildren(subjectList,f.getId()));
        }
        if (ObjectUtil.isNotEmpty(first) && first.size()>0){
            return new Result(2000,"成功",first);
        }
        return new Result(0000,"失败");
    }

}
